﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace SMSPanasonic.Business
{
    public partial class pa_WarrantyPackage
    {
    	#region public properties
		public int WarrantyPackageID { get; set; }
		public string WarrantyPackageCode { get; set; }
		public string WarrantyPackageName { get; set; }
		public int WarrantyPeriod { get; set; }
		public string PeriodType { get; set; }
		public string Description { get; set; }
		public bool? Disabled { get; set; }
		#endregion

		#region constructor
		public pa_WarrantyPackage(){ }

		public pa_WarrantyPackage(IDataReader rd)
		{
			this.WarrantyPackageID = (int)rd["WarrantyPackageID"];
			this.WarrantyPackageCode = rd["WarrantyPackageCode"].ToString();
			this.WarrantyPackageName = rd["WarrantyPackageName"].ToString();
			this.WarrantyPeriod = (int)rd["WarrantyPeriod"];
			this.PeriodType = rd["PeriodType"].ToString();
			this.Description = rd["Description"].ToString();
			this.Disabled = rd["Disabled"].Equals(DBNull.Value) ? (bool?)null : (bool)rd["Disabled"];
		}
		#endregion

		#region static method
        public static pa_WarrantyPackage GetOne(string package)
        {
            using (SqlDataReader rd = SqlHelper.ExecuteReader(AppConfig.ConnectionString, CommandType.Text,
                        "SELECT * FROM pa_WarrantyPackage WHERE WarrantyPackageCode = @WarrantyPackageCode",
                        new SqlParameter("@WarrantyPackageCode", package)))
            {
                while (rd.Read())
                {
                    return new pa_WarrantyPackage(rd);
                }
                return null;
            }
        }

        public static bool CheckExists(string package)
        {
            return GetOne(package) != null;
        }

        public static DateTime GetExpriredDate(string package, pa_WarrantyWaiting objWaiting)
        {
            DateTime? registerDate = pa_Warranty.GetWarrantyExtentRegisterDate(objWaiting.ModelCode, objWaiting.ProductCode, objWaiting.RegisterDate);
            if (registerDate != null)
            {
                return registerDate.Value.AddMonths(pa_WarrantyPackage.GetOne(package).WarrantyPeriod).GetLastDayOfMonth();
            }
            return objWaiting.RegisterDate.AddMonths(pa_WarrantyPackage.GetOne(package).WarrantyPeriod).GetLastDayOfMonth();
        }
		#endregion
	}
}
